package edu.sysu.cshare.util.gui;

import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JToolTip;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalToolTipUI;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

public class GUI {

	public static void showModalDialog(JDialog dialog) {
		dialog.toFront();
		dialog.setModal(true);
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setVisible(true);
	}

	public static void showMessageDialog(String msg) {
		JOptionPane.showMessageDialog(null, msg);
	}

	public static void setUIFont(javax.swing.plaf.FontUIResource f) {
		java.util.Enumeration keys = UIManager.getDefaults().keys();
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof javax.swing.plaf.FontUIResource)
				UIManager.put(key, f);
		}
	}

	public static String getDateTooltip() {
		return "日期格式形如 \"2012-12-23 00时\"";
	}

	public static String getDirPath() throws Exception {
		JFileChooser chs = new JFileChooser();
		chs.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		if (chs.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
			return chs.getSelectedFile().toString();
		}
		throw new Exception("路径无效");
	}

	public static String getFilePath() throws Exception {
		JFileChooser chs = new JFileChooser();
		if (chs.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
			return chs.getSelectedFile().toString();
		}
		throw new Exception("路径无效");

	}

	public static void FitTableColumns(JTable myTable) { // 設置table的列寬隨內容調整

		JTableHeader header = myTable.getTableHeader();
		int rowCount = myTable.getRowCount();
		Enumeration columns = myTable.getColumnModel().getColumns();
		while (columns.hasMoreElements()) {
			TableColumn column = (TableColumn) columns.nextElement();
			int col = header.getColumnModel().getColumnIndex(
					column.getIdentifier());
			int width = (int) myTable
					.getTableHeader()
					.getDefaultRenderer()
					.getTableCellRendererComponent(myTable,
							column.getIdentifier(), false, false, -1, col)

					.getPreferredSize().getWidth();

			for (int row = 0; row < rowCount; row++) {
				int preferedWidth = (int) myTable
						.getCellRenderer(row, col)
						.getTableCellRendererComponent(myTable,
								myTable.getValueAt(row, col), false, false,
								row, col).getPreferredSize().getWidth();
				width = Math.max(width, preferedWidth);

			}
			header.setResizingColumn(column);
			column.setWidth(width + myTable.getIntercellSpacing().width);

		}

	}

	public static class MultiLineToolTip extends JToolTip {
		public MultiLineToolTip() {
			setUI(new MultiLineToolTipUI());
		}

		private class MultiLineToolTipUI extends MetalToolTipUI {
			private String[] strs;
			private int maxWidth = 0;

			@Override
			public void paint(Graphics g, JComponent c) {
				FontMetrics metrics = c.getFontMetrics(g.getFont());
				Dimension size = c.getSize();
				g.setColor(c.getBackground());
				g.fillRect(0, 0, size.width, size.height);
				g.setColor(c.getForeground());
				if (strs != null) {
					for (int i = 0; i < strs.length; i++) {
						g.drawString(strs[i], 3, (metrics.getHeight())
								* (i + 1));
					}
				}
			}

			@Override
			public Dimension getPreferredSize(JComponent c) {
				FontMetrics metrics = Toolkit.getDefaultToolkit()
						.getFontMetrics(c.getFont());
				String tipText = ((JToolTip) c).getTipText();
				if (tipText == null) {
					tipText = " ";
				}
				BufferedReader br = new BufferedReader(
						new StringReader(tipText));
				String line;
				int maxWidth = 0;
				Vector v = new Vector();
				try {
					while ((line = br.readLine()) != null) {
						int width = SwingUtilities.computeStringWidth(metrics,
								line);
						maxWidth = (maxWidth < width) ? width : maxWidth;
						v.addElement(line);
					}
				} catch (IOException ex) {
					ex.printStackTrace();
				}
				int lines = v.size();
				if (lines < 1) {
					strs = null;
					lines = 1;
				} else {
					strs = new String[lines];
					int i = 0;
					for (Enumeration e = v.elements(); e.hasMoreElements(); i++) {
						strs[i] = (String) e.nextElement();
					}
				}
				int height = metrics.getHeight() * lines;
				this.maxWidth = maxWidth;
				return new Dimension(maxWidth + 6, height + 4);
			}
		}
	}

}
